window-or-global
Use this module to get the global object both on server and client side.
No more window is not defined
errors just peace and flowers.
Advantages:
- You can easily mock it by mockery or proxyquire in your tests
- It's really useful in case of universal (or isomorphic) code, for example, when you'd like to render a React component both on client and server side
Install
$ npm i window-or-global
Usage
import root from 'window-or-global'
React example
components/my-component.jsx:
import React, { Component } from 'react'
import root from 'window-or-global'
class MyComponent extends Component {
componentDidMount() {
root.addEventListener()
}
componentWillUnmount() {
root.addEventListener()
}
render() {}
}
Unit-testing example
You can find a complete test example here. The test passes of course, just type
$ npm test
in your terminal.
License
MIT © Purpose Industries